/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package system;

import Main.Parameters;
import java.io.BufferedWriter;
import java.io.FileWriter;

/**
 *
 * @author ZeM
 */
public class Logging {

    static BufferedWriter out;

    /**
     * Creating main log file
     */
    public void CreateLogFile() {
        try {
            out = new BufferedWriter(new FileWriter(Parameters.MainLogPath));
            out.write("<?xml version=" + '"' + "1.0" + '"' + "encoding=" + '"' + "UTF-8" + '"' + "?>");
            out.newLine();
            out.write("<log>");
            out.newLine();
            out.flush();
            Parameters.MainLogCreated = true;
        } catch (Exception error) {
        }

    }

    /**
     * adding message to log file
     * @param message
     */
    public void AddMessage(String message) {

        if (Parameters.MainLogCreated) {

            try {
                out.write(" <record>");
                out.newLine();
                out.write("\t<date>" + new getTimeDate().getTimeDate("yyyy/MM/dd") + "</date>");
                out.newLine();
                out.write("\t<time>" + new getTimeDate().getTimeDate("HH:mm:ss.SSS") + "</time>");
                out.newLine();
                out.write("\t\t<message>" + message + "</message>");
                out.newLine();
                out.write(" </record>");
                out.newLine();
              out.flush();
            } catch (Exception error) {
                System.out.println(error);
            }
        }

    }

    /**
     *  adding event to log file
     * @param message
     */
    public void AddEvent(String message){
        if (Parameters.MainLogCreated){
         try {
                out.write(" <event>");
                out.newLine();
                out.write("\t<date>" + new getTimeDate().getTimeDate("yyyy/MM/dd") + "</date>");
                out.newLine();
                out.write("\t<time>" + new getTimeDate().getTimeDate("HH:mm:ss.SSS") + "</time>");
                out.newLine();
                out.write("\t\t<message>" + message + "</message>");
                out.newLine();
                out.write(" </event>");
                out.newLine();
              out.flush();
            } catch (Exception error) {
                System.out.println(error);
            }

        }

    }

    /**
     * close log file
     */
    public void Close() {
        if (Parameters.MainLogCreated) {
            try {
                out.write("</log>");
                out.flush();
                out.close();
            } catch (Exception error) {
            }
        }
    }
}
